package com.brotherly.mpgp.widget;

import java.util.Map;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.widget.RemoteViews;

import com.brotherly.framework.http.HttpListener;
import com.brotherly.framework.http.HttpService;
import com.brotherly.framework.service.ServiceManager;
import com.brotherly.mpgp.MainActivity;
import com.brotherly.mpgp.R;
import com.brotherly.util.BitmapUtil;
import com.brotherly.util.InternetResourceCache;

public class GameAssistantWidgetProvider extends AppWidgetProvider {

    private static final String CLICK = "com.brotherly.mpgp.widget.click";

    private static RemoteViews rv;

    private String mContent = null;
    private String mImagePath = null;

    private Context mContext = null;

    @Override
    public void onDeleted(Context context, int[] appWidgetIds)
    {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context)
    {
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context)
    {
        super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent)
    {
        super.onReceive(context, intent);

        mContext = context;

        if (intent.getStringExtra("intents") != null)
        {
            if (mContent == null && intent.getStringExtra("content") == null)
            {
                mContent = "手游秘籍是针对目前市面热门手机网游为玩家提供精品攻略活动服务";
            } else
            {
                mContent = intent.getStringExtra("content");
            }
            mImagePath = intent.getStringExtra("imagePath");
            updateWidget(context);
        }

        // try
        // {
        // // String intents = intent.getExtras().getString("intents");
        // String content = intent.getExtras().getString("content");
        // String imagePath = intent.getExtras().getString("imagePath");
        //
        // // if ("com.brotherly.mpgp.MainActivity".equals(intents))
        // // {
        // RemoteViews remoteViews = new
        // RemoteViews(context.getPackageName(),
        // R.layout.game_assistant);
        // remoteViews.setTextViewText(R.id.tv_game_assistant_content,
        // content);
        // PendingIntent pi = PendingIntent.getActivity(context, 0, new
        // Intent(context, MainActivity.class), 0);
        //
        // remoteViews.setOnClickPendingIntent(R.id.pushLayout, pi);
        // AppWidgetManager appWidgetManager =
        // AppWidgetManager.getInstance(context);
        // ComponentName componentName = new ComponentName(context,
        // GameAssistantWidgetProvider.class);
        // appWidgetManager.updateAppWidget(componentName, remoteViews);
        // // }
        // // else if
        // // ("com.brotherly.mpgp.ArticleListActivity".equals(intents))
        // // {
        // // RemoteViews remoteViews = new
        // // RemoteViews(context.getPackageName(),
        // // R.layout.game_assistant);
        // // remoteViews.setTextViewText(R.id.tv_game_assistant_content,
        // // content);
        // // AppWidgetManager appWidgetManager =
        // // AppWidgetManager.getInstance(context);
        // // ComponentName componentName = new ComponentName(context,
        // // GameAssistantWidgetProvider.class);
        // // appWidgetManager.updateAppWidget(componentName, remoteViews);
        // // }
        //
        // }
        // catch (Exception e)
        // {
        //
        // }

    }

    private void updateWidget(Context context)
    {
        RemoteViews remoteViews = new
                RemoteViews(context.getPackageName(),
                        R.layout.game_assistant);
        if (mContent != null)
        {
            remoteViews.setTextViewText(R.id.tv_game_assistant_content,
                    mContent);
        }
        if (mImagePath != null)
        {
            String cachePath = InternetResourceCache.getInstance().getCacheFilename(mImagePath);
            if (cachePath != null)
            {
                Bitmap b = BitmapUtil.loadBitmap(cachePath);
                remoteViews.setImageViewBitmap(R.id.iv_game_assistant_image, b);
            } else
            {
                remoteViews.setImageViewResource(R.id.iv_game_assistant_image, R.drawable.ic_launcher);
                HttpService httpService = (HttpService) ServiceManager.getInstance().getService(ServiceManager.HTTP_SERVICE);
                InternetResourceCache.getInstance().cacheHttpResource(mImagePath, "get", null, null, mHttpListener, false,
                        httpService);
            }
        } else
        {
            remoteViews.setImageViewResource(R.id.iv_game_assistant_image, R.drawable.ic_launcher);
        }
        PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

        remoteViews.setOnClickPendingIntent(R.id.pushLayout, pi);
        AppWidgetManager appWidgetManager =
                AppWidgetManager.getInstance(context);
        ComponentName componentName = new ComponentName(context,
                GameAssistantWidgetProvider.class);
        appWidgetManager.updateAppWidget(componentName, remoteViews);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
    {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    private HttpListener mHttpListener = new HttpListener()
    {
        @Override
        public void result(int code, Map<String, String> headers, byte[] body)
        {
            updateWidget(mContext);
            // new Handler().post(new Runnable()
            // {
            // @Override
            // public void run()
            // {
            // }
            // });
        }
    };
}
